# 组合数据类型

# 序列类型 字符串、列表、元组
# s = [1, 2, "Yu"]
# print(s)
# print(s * 2)
# t = (3, 4)
# print(t + t)
# print(s[0:2])  # 1 2
# print(s[0:2:2])  # 1  上面的就类似于步长为1
# # print(max(s)) #注意，此序列之间必须只存在一种可比的数据类型，像这里这个str类型和int类型就不可比
# s[2] = 3
# print(max(s))
# print(s.index(3))
# # print(s.index(3, 0, 2))  # 返回异常 s的[0, 2)区域里没有3这个元素
# print(s.count(3))

# 列表操作
# del
# ls = [1, 2, 3]
# print(ls)
# del ls[1]
# print(ls)
# del ls[0:1]
# print(ls)  # 3

# copy
# ls = [1, 2, 3]
# lls = ls  # 注意这里的赋值没有拷贝赋值 -- 单纯的共享一块区域，类似于C++的引用
# lls[0] = 0
# print(lls, ls)
# lls2 = ls.copy()  # 此时才是拷贝，开辟的一个新的空间
# lls2[0] = 1
# print(ls, lls2)
# ls.reverse()  # 反转
# print(ls)
# ls.sort()  # 排序
# print(ls)
# ls.clear()
# print(ls)

# ls = [1, 2, 3]
# ls += [4, 5]
# print(ls)
# ls.extend([6, 7, 8])
# print(ls)
# # ls += 9
# print(ls)

# 遍历列表
# ls = range(1, 6)
# print(type(ls))  # 注意虽然是创建一个列表，但是类型还是range，需要进行类型转换
# ls = list(ls)
# print(type(ls))  # 此时就是一个列表了
# print(ls)

# lis = [1, 2, "Yu"]
# for tmp in lis:
#     print(tmp, end=" ")
# print()
# i = 0
# while i < len(lis):
#     print(lis[i], end=" ")
#     i += 1

# 字典类型
# dict = {(1, 2): "2"}
# print(dict)

# dict1 = {1: "Yu", 2: "shen", '三': 3}
# print(dict1)
# dict2 = dict(一="YU", 二="shen", 三=3)  # 关键字进行构造字典，键均为字符串
# print(dict2)
# dict3 = dict([(1, "Yu"), (2, "shen"), ('三', 3)])  # 利用序列进行创建字典对象
# print(dict3)
# print(1 in dict3)  # True
# print(dict3[2], dict3["三"])

# dict1 = {1: "Yu", 2: "shen", '三': 3}
# # 将'三'对应的值修改为"special"，然后添加3键，对应值为"666"
# dict1['三'] = "special"  # 修改
# dict1[3] = "666"  # 添加
# print(dict1)

# 字典常用方法
# dicts = dict([(1, "Yu"), (2, "shen"), ('三', 3)])
# print(dicts.keys())  # 返回所有键信息
# print(dicts.values())  # 返回所有值信息
# print(dicts.items())  # 返回所有键值对
# print("----------------------")
#
# print(dicts.get(2))  # 返回对应值
# print(dicts.get(3, "不存在"))  # 键存在返回相应值，否则返回默认值
# print(dicts.pop('三'))
# print(dicts)
# print("----------------------")
#
# print(dicts.popitem())  # 随机从字典取出一个键值对，以元组形式返回(key, value)
# print(dicts)
# dicts.clear()  # 删除所有的键值对
# print(dicts)
# print("----------------------")
#
# dicts[1] = "Yu"
# dicts[2] = "shen"
# dicts['三'] = 3
# print(dicts)
# del dicts['三']  # 删除'三'这个键对应的键值对
# print(dicts)
# print("----------------------")
#
# dicts1 = dicts  # 此时dicts1不是全新的空间，而是dicts的别名而已
# print(dicts1)
# dicts['三'] = 3
# print(dicts1)  # dicts发生变化dicts1也会发生变化
# dicts1 = dicts.copy()  # 此时就是全新的空间了
# del dicts['三']
# print(dicts, dicts1)
# print("----------------------")
#
# # 此时将dicts1换成dicts的值，并且不是共享
# dicts[3] = "666"
# dicts1.update(dicts)  # 更新内容,即添加dicts1里没有dicts里的元素
# print(dicts1)
# del dicts1[2]
# print(dicts, dicts1)  # 证明不是同一个空间

# 集合
# set1 = set()  # 空集合
# set2 = set("abcde")  # 以字符串可迭代对象进行创建集合
# set3 = set([1, 2, 3, '四'])
# print(set1, set2, set3)

# set1 = set()  # 空集合
# set1.add(1)  # 添加1这个元素
# print(set1)
# set1.add(1)  # 添加重复元素
# set1.add(2)  # 添加不重复元素
# print(set1)
# print("----------------------")
#
# set2 = set1.copy()  # 复制拷贝一个新的集合
# set1.clear()  # 对set1进行清空操作
# print(set1, set2)
# print("----------------------")
#
# print(set2.pop(), set2)  # 随机输出一个集合里面的元素，并且消除
# print("----------------------")
#
# set2.add(1)
# set2.add(3)
# set2.discard(1)  # 有此元素就进行移除
# print(set2)
# set2.discard(6)  # 无此元素 不报异常
# # set2.remove(6)  # 无此元素，报异常
# print("----------------------")
#
# print(set2.isdisjoint(set1))  # 不存在相同元素
# set1.add(2)
# print(set2.isdisjoint(set1))  # 存在相同元素
# print(len(set1), len(set2))
# print("----------------------")

# 集合的遍历
# set1 = set("abcdea")
# for tmp in set1:
#     print(tmp, end=",")
# print()

# 利用集合去去重：
# lis = [1, 1, 2, 2, 3, 4, 4]
# for i in lis:
#     print(i, end=" ")
# print()
# set1 = set(lis)
# lis.clear()
# lis = list(set1)
# for i in lis:
#     print(i, end=" ")

# Python中的set进行数学中的运算
# set1 = set([1, 2, 4, 5, 6, 7])
# set2 = set([1, 2, 4, 8])
#
# set3 = set1 & set2  # 计算交集1 2 4
# print(set3)
# set4 = set1 | set2  # 计算并集1 2 4 5 6 7 8
# print(set4)
# set5 = set1 ^ set2  # 计算补集5 6 7 8
# print(set5)
# set6 = set1 - set2  # 计算差集 5 6 7
# print(set6)
# set2.remove(8)
# print(set2 <= set1)  # 测试set2是否为set1的子集
# print(set1 >= set2)

# 二分查找
# lis = [7, -1, 2, 4, 7, 9, 1, 0, 8]
# # 利用二分查找首先就要进行排序：
# lis.sort()
# print(lis)
# pos = eval(input("输入你要查找的整数："))
# left = 0
# right = len(lis) - 1
# flag = True
# while left <= right:
#     mid = (right - left) // 2 + left
#     if lis[mid] == pos:
#         flag = False
#         print("找到了，下标为{}".format(mid))
#         break
#     elif lis[mid] > pos:
#         right = mid - 1
#     else:
#         left = mid + 1
# if flag:
#     print("没有找到哦")

# print(type({'1', 2, 3}))

# temp = [1, 2, 3, None, ]
# print(len(temp))
# print(temp[2], temp[3])

# s = {'a': 1, 'b': 2}
# s = dict([('a', 1), ('b', 2)])
# # print(s[b])
# print(s['b'])

# 编程题：
# 随机生成由英文字符和数字组成的四位验证码
# import random
# _str = ""
# i = 0
# while i < 4:
#     pos = random.randint(48, 122)
#     if (pos <= 57) or (65 <= pos <= 90) or (97 <= pos):
#         _str += chr(pos)
#         i += 1
# print(_str)

# 字典描述信息：学号、姓名、成绩 列表储存信息
# lis1 = ["2108050210", "2108050211", "2108050212"]
# lis2 = ["张三", "王五", "李四"]
# lis3 = [99, 98, 23]
# dicts = dict([("no", lis1), ("name", lis2), ("score", lis3)])
# name = input("请输入想要查询学生姓名：")
# tmp = dicts["name"]
# i = 0
# while i < len(tmp):
#     if tmp[i] == name:
#         print("no {}  name {}  score {}".format(dicts["no"][i], name, dicts["score"][i]))
#         break
#     i += 1
# else:
#     print("查无此人")

#
# _str = input("请输入若干单词：")
# lis = _str.split(" ")
# lis = list(set(lis))
# lis.sort()
# for i in lis:
#     print(i)

# 用元组创建一个用来储存Python关键字的对象，然后检查输入的单词是否为关键字：
# keywords = ("and", "as", "assert", "break", "class", "continue", "def", "del", "elif")
# pos = input("请输入单词:")
# for s in keywords:
#     if s == pos:
#         print("输入的是关键字哦~")
#         break
# else:
#     print("输入的不是关键字")

# 编写程序，删除list中的重复元素
# lis = [1, 1, 2, 3, 3, 4, 5, 1, 6, 2, 9]
# print(lis)
# print(list(set(lis)))




